package com.mobile.check.util;

import java.util.Locale;

/**
 * 16进制值与String/Byte之间的转换
 * @author JerryLi
 * @email lijian@dzs.mobi
 * @data 2011-10-16
 * */
public class CHexConver {
    public static byte[] end = new byte[]{0, 3};
    public static byte[] heade1 = new byte[]{2, 0, 9, 38};
    public static byte[] header = new byte[]{2, 0, 8, 38};
    private static final char[] mChars = "0123456789ABCDEF".toCharArray();
    private static final String mHexStr = "0123456789ABCDEF";

    public CHexConver() {
    }

    public static String byte2HexStr(byte[] var0, int var1) {
        StringBuilder var3 = new StringBuilder();

        for(int var2 = 0; var2 < var1; ++var2) {
            var3.append(mChars[(var0[var2] & 255) >> 4]);
            var3.append(mChars[var0[var2] & 15]);
            var3.append(' ');
        }

        return var3.toString().trim().toUpperCase(Locale.US);
    }

    public static boolean checkHexStr(String var0) {
        var0 = var0.toString().trim().replace(" ", "").toUpperCase(Locale.US);
        int var2 = var0.length();
        if (var2 > 1 && var2 % 2 == 0) {
            int var1 = 0;

            while(true) {
                if (var1 >= var2) {
                    return true;
                }

                if (!"0123456789ABCDEF".contains(var0.substring(var1, var1 + 1))) {
                    break;
                }

                ++var1;
            }
        }

        return false;
    }

    public static String convertData(byte[] var0) {
        String var2 = "";
        String var1;
        if (!startWith(var0, header)) {
            var1 = var2;
            if (!startWith(var0, heade1)) {
                return var1;
            }
        }

        var1 = var2;
        if (endWidth(var0, end)) {
            var1 = new String(removeEnd(removeHeader(var0, header), end));
        }

        return var1;
    }

    public static boolean endWidth(byte[] var0, byte[] var1) {
        if (var0 != null && var0.length != 0 && var1 != null && var1.length != 0 && var0.length >= var1.length) {
            int var3 = var1.length - 1;
            int var2 = var0.length - 1;

            while(true) {
                if (var3 < 0) {
                    return true;
                }

                if (var1[var3] != var0[var2]) {
                    break;
                }

                --var3;
                --var2;
            }
        }

        return false;
    }

    public static byte[] hexStr2Bytes(String var0) {
        var0 = var0.trim().replace(" ", "").toUpperCase(Locale.US);
        int var2 = var0.length() / 2;
        byte[] var4 = new byte[var2];

        for(int var1 = 0; var1 < var2; ++var1) {
            int var3 = var1 * 2 + 1;
            var4[var1] = (byte)(Integer.decode("0x" + var0.substring(var1 * 2, var3) + var0.substring(var3, var3 + 1)) & 255);
        }

        return var4;
    }

    public static String hexStr2Str(String var0) {
        String var2 = var0.toString().trim().replace(" ", "").toUpperCase(Locale.US);
        char[] var3 = var2.toCharArray();
        byte[] var4 = new byte[var2.length() / 2];

        for(int var1 = 0; var1 < var4.length; ++var1) {
            var4[var1] = (byte)(("0123456789ABCDEF".indexOf(var3[var1 * 2]) << 4 | "0123456789ABCDEF".indexOf(var3[var1 * 2 + 1])) & 255);
        }

        return new String(var4);
    }

    public static byte[] removeEnd(byte[] var0, byte[] var1) {
        int var2 = var0.length - var1.length;
        var1 = new byte[var2];
        System.arraycopy(var0, 0, var1, 0, var2);
        return var1;
    }

    public static byte[] removeHeader(byte[] var0, byte[] var1) {
        int var2 = var0.length - var1.length;
        byte[] var3 = new byte[var2];
        System.arraycopy(var0, var1.length, var3, 0, var2);
        return var3;
    }

    public static boolean startWith(byte[] var0, byte[] var1) {
        if (var0 != null && var0.length != 0 && var1 != null && var1.length != 0 && var0.length >= var1.length) {
            int var2 = 0;

            while(true) {
                if (var2 >= var1.length) {
                    return true;
                }

                if (var1[var2] != var0[var2]) {
                    break;
                }

                ++var2;
            }
        }

        return false;
    }

    public static String str2HexStr(String var0) {
        StringBuilder var2 = new StringBuilder();
        byte[] var3 = var0.getBytes();

        for(int var1 = 0; var1 < var3.length; ++var1) {
            var2.append(mChars[(var3[var1] & 255) >> 4]);
            var2.append(mChars[var3[var1] & 15]);
            var2.append(' ');
        }

        return var2.toString().trim();
    }

    public static String strToUnicode(String var0) throws Exception {
        StringBuilder var3 = new StringBuilder();

        for(int var1 = 0; var1 < var0.length(); ++var1) {
            char var2 = var0.charAt(var1);
            String var4 = Integer.toHexString(var2);
            if (var2 > 128) {
                var3.append("\\u");
            } else {
                var3.append("\\u00");
            }

            var3.append(var4);
        }

        return var3.toString();
    }

    public static String unicodeToString(String var0) {
        int var2 = var0.length() / 6;
        StringBuilder var3 = new StringBuilder();

        for(int var1 = 0; var1 < var2; ++var1) {
            String var4 = var0.substring(var1 * 6, (var1 + 1) * 6);
            var3.append(new String(Character.toChars(Integer.valueOf(var4.substring(2, 4), 16) << 8 | Integer.valueOf(var4.substring(4), 16))));
        }

        return var3.toString();
    }
}
